package com.qt.dispatcher;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.qt.receiver.JudgeReceiver;
import com.qt.utils.Constants;
import com.qt.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author pgw
 * @version 1.0
 * @description
 * @date Create by 2023/9/25 20:03
 */
@Component
@Slf4j(topic = "qtoj")
public class JudgeDispatcher {

    @Autowired
    private RedisUtil redisUtil;


    @Autowired
    private JudgeReceiver judgeReceiver;




    public void sendTask(Long judgeId, Long problemId, boolean isContestSubmit) {
       JSONObject task = new JSONObject();
       task.set("judgeId" , judgeId );
       task.set("problemId" , problemId);
       task.set("isContestSubmit" , isContestSubmit);

       boolean isOk;
       if(isContestSubmit){
           isOk = redisUtil.llPush(Constants.Queue.CONTEST_JUDGE_WAITING.getName() , JSONUtil.toJsonStr(task));
       }else{
           isOk = redisUtil.llPush(Constants.Queue.GENERAL_JUDGE_WAITING.getName(), JSONUtil.toJsonStr(task));
       }

       if(!isOk){

       }

        // 调用判题任务处理
        judgeReceiver.processWaitingTask();

    }




}
